package com.github.linkoog.model_designer.ide.editer.dbtable.tab_editer;

import com.github.linkoog.model_designer.common.JCheckBoxTableCellEditor;
import com.github.linkoog.model_designer.common.JCheckBoxTableCellRenderer;
import com.github.linkoog.model_designer.common.JComboEnumTableCellEditor;
import com.github.linkoog.model_designer.common.TableModelEditor;
import com.github.linkoog.model_designer.ide.editer.common.ColumnVo;
import com.github.linkoog.model_designer.ide.editer.common.DataType;
import com.intellij.util.Function;
import com.intellij.util.ui.ColumnInfo;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import java.awt.*;

public class ColumnsTableEditer extends TableModelEditor<ColumnVo> {

    public ColumnsTableEditer() {
        super(COLUMNS, itemEditor, "No column configured");
        table.getTableHeader().setReorderingAllowed(false);//禁止拖动表头

        DefaultTableCellRenderer rightTableCellRenderer = new DefaultTableCellRenderer();
        rightTableCellRenderer.setHorizontalAlignment(SwingConstants.RIGHT);
        final TableColumn column0 = table.getColumnModel().getColumn(0);
        column0.setResizable(false);
        column0.setHeaderRenderer(rightTableCellRenderer);

        table.setUpdateSelectionOnSort(false);
        table.setAutoCreateRowSorter(false);

        table.setDragEnabled(false);

        DefaultTableCellRenderer defaultTableCellRenderer = new DefaultTableCellRenderer();
        defaultTableCellRenderer.setHorizontalAlignment(SwingConstants.CENTER);
        table.getTableHeader().setDefaultRenderer(defaultTableCellRenderer);
    }

    @Override
    public ColumnVo createElement() {
        return ColumnVo.newStringColumnVo();
    }

    private static final ColumnInfo[] COLUMNS = {
            new TableModelEditor.EditableColumnInfo<ColumnVo, ColumnVo>("#") {
                @NotNull
                @Override
                public ColumnVo valueOf(ColumnVo columnVo) {
                    return columnVo;
                }

                @Override
                public void setValue(ColumnVo columnVo, ColumnVo value) {
                }

                @Override
                public boolean isCellEditable(ColumnVo columnVo) {
                    return false;
                }

                @Override
                public TableCellRenderer getRenderer(ColumnVo columnVo) {
                    //return new RowNumCellRenderer();
                    DefaultTableCellRenderer renderer = new DefaultTableCellRenderer(){
                        @Override
                        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
                            super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
                            this.setText("" + (row + 1));
                            return this;
                        }
                    };
                    renderer.setHorizontalAlignment(SwingConstants.CENTER);
                    return renderer;
                }

                @Override
                public int getWidth(JTable table) {
                    return 38;
                }
            },

            new TableModelEditor.EditableColumnInfo<ColumnVo, String>("名称") {
                @Override
                public int getWidth(JTable table) {
                    return 115;
                }

                @Override
                public String valueOf(ColumnVo columnVo) {
                    return columnVo.getName();
                }

                @Override
                public void setValue(ColumnVo columnVo, String value) {
                    columnVo.setName(value);
                }

                @Override
                public boolean isCellEditable(ColumnVo columnVo) {
                    return true;
                }
            },

            new ColumnInfo<ColumnVo, String>("数据类型") {
                @Override
                public int getWidth(JTable table) {
                    return 105;
                }

                @Override
                public Class getColumnClass() {
                    return String.class;
                }

                @Override
                public String valueOf(ColumnVo columnVo) {
                    return columnVo.getDataType().getValue();
                }

                @Override
                public void setValue(ColumnVo columnVo, String value) {
                    columnVo.setDataType(DataType.valueOfName(value));
                }

                @Override
                public boolean isCellEditable(ColumnVo columnVo) {
                    return true;
                }

                @Override
                public TableCellEditor getEditor(ColumnVo columnVo) {
                    JComboEnumTableCellEditor jComboEnumTableCellEditor = new JComboEnumTableCellEditor(DataType.class, false);
                    return jComboEnumTableCellEditor;
                }

               /*@Override
                public TableCellRenderer getRenderer(ColumnVo columnVo) {
                    DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
                    renderer.setHorizontalAlignment(SwingConstants.CENTER);
                    return renderer;
                }*/
            },

            new TableModelEditor.EditableColumnInfo<ColumnVo, Integer>("长度") {
                @Override
                public Class getColumnClass() {
                    return Integer.class;
                }

                @Override
                public Integer valueOf(ColumnVo columnVo) {
                    return columnVo.getLength();
                }

                @Override
                public void setValue(ColumnVo columnVo, Integer value) {
                    columnVo.setLength(value);
                }

                @Override
                public boolean isCellEditable(ColumnVo columnVo) {
                    return true;
                }

                @Override
                public int getWidth(JTable table) {
                    return 45;
                }

                @Override
                public TableCellRenderer getRenderer(ColumnVo columnVo) {
                    DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
                    renderer.setHorizontalAlignment(SwingConstants.CENTER);
                    return renderer;
                }
            },

            new TableModelEditor.EditableColumnInfo<ColumnVo, Integer>("小数点") {
                @Override
                public Class getColumnClass() {
                    return Integer.class;
                }

                @Override
                public Integer valueOf(ColumnVo columnVo) {
                    return columnVo.getRadix();
                }

                @Override
                public void setValue(ColumnVo columnVo, Integer value) {
                    columnVo.setRadix(value);
                }

                @Override
                public boolean isCellEditable(ColumnVo columnVo) {
                    return true;
                }

                @Override
                public int getWidth(JTable table) {
                    return 45;
                }

                @Override
                public TableCellRenderer getRenderer(ColumnVo columnVo) {
                    DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
                    renderer.setHorizontalAlignment(SwingConstants.CENTER);
                    return renderer;
                }
            },


            new TableModelEditor.EditableColumnInfo<ColumnVo, Boolean>("Not Null") {
                @Override
                public Class getColumnClass() {
                    return Boolean.class;
                }

                @Override
                public Boolean valueOf(ColumnVo columnVo) {
                    return columnVo.getNotNull();
                }

                @Override
                public void setValue(ColumnVo columnVo, Boolean value) {
                    columnVo.setNotNull(value);
                }

                @Override
                public boolean isCellEditable(ColumnVo columnVo) {
                    return true;
                }

                @Override
                public TableCellRenderer getRenderer(ColumnVo columnVo) {
                    return new JCheckBoxTableCellRenderer();
                }

                @Override
                public TableCellEditor getEditor(ColumnVo columnVo) {
                    return new JCheckBoxTableCellEditor();
                }

                @Override
                public int getWidth(JTable table) {
                    return 60;
                }
            },

            new TableModelEditor.EditableColumnInfo<ColumnVo, Boolean>("主键") {
                @Override
                public Class getColumnClass() {
                    return Boolean.class;
                }

                @Override
                public Boolean valueOf(ColumnVo columnVo) {
                    return columnVo.getPrimaryKey();
                }

                @Override
                public void setValue(ColumnVo columnVo, Boolean value) {
                    columnVo.setPrimaryKey(value);
                }

                @Override
                public boolean isCellEditable(ColumnVo columnVo) {
                    return true;
                }

                @Override
                public TableCellRenderer getRenderer(ColumnVo columnVo) {
                    return new JCheckBoxTableCellRenderer();
                }

                @Override
                public TableCellEditor getEditor(ColumnVo typeMapping) {
                    return new JCheckBoxTableCellEditor();
                }

                @Override
                public int getWidth(JTable table) {
                    return 50;
                }
            },

            new TableModelEditor.EditableColumnInfo<ColumnVo, String>("默认值") {
                @Override
                public int getWidth(JTable table) {
                    return 115;
                }

                @Override
                public String valueOf(ColumnVo columnVo) {
                    return columnVo.getDefaultValue();
                }

                @Override
                public void setValue(ColumnVo columnVo, String value) {
                    columnVo.setDefaultValue(value);
                }

                @Override
                public boolean isCellEditable(ColumnVo columnVo) {
                    return true;
                }
            },

            new TableModelEditor.EditableColumnInfo<ColumnVo, String>("注释") {
                @Override
                public String valueOf(ColumnVo columnVo) {
                    return columnVo.getComment();
                }

                @Override
                public void setValue(ColumnVo columnVo, String value) {
                    columnVo.setComment(value);
                }

                @Override
                public boolean isCellEditable(ColumnVo columnVo) {
                    return true;
                }
            },
    };

    private static final TableModelEditor.DialogItemEditor<ColumnVo> itemEditor = new TableModelEditor.DialogItemEditor<ColumnVo>() {
        @NotNull
        @Override
        public Class<ColumnVo> getItemClass() {
            return ColumnVo.class;
        }

        @Override
        public ColumnVo clone(@NotNull ColumnVo old, boolean forInPlaceEditing) {
            return ColumnVo.copy(old);
        }

        @Override
        public void edit(@NotNull ColumnVo item, @NotNull Function<? super ColumnVo, ? extends ColumnVo> mutator, boolean isAdd) {
            /*BrowserSpecificSettings settings = cloneSettings(browser);
                if (settings != null && ShowSettingsUtil.getInstance().editConfigurable(browsersTable, settings.createConfigurable())) {
                    mutator.fun(browser).setSpecificSettings(settings);
                }*/
        }

        @Override
        public void applyEdited(@NotNull ColumnVo oldItem, @NotNull ColumnVo newItem) {
            //oldItem.setSpecificSettings(newItem.getSpecificSettings());
        }

        @Override
        public boolean isEditable(@NotNull ColumnVo browser) {
            //browser.getSpecificSettings() != null;
            return false;
        }

        @Override
        public boolean isRemovable(@NotNull ColumnVo item) {
            //!WebBrowserManager.getInstance().isPredefinedBrowser(item);
            return true;
        }
    };

}
